﻿(function() {
	var LID;
	function init() {
		var s, head, btn, confirm_btn;
		btn = document.createElement("div");
		btn.className = "touzhu2";
		btn.onclick = function() {
			refreshd($("#LID").val());
		};
		btn.textContent = "投注";
		confirm_btn = $(".border_out1_r");
		confirm_btn.append(btn);
		$(".touzhu2").eq(1).remove();
	}

	function clear() {
		$(".img_bt1").css("background-image", "url(/img/pg28/img_bt1.gif)");
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				$(this).val("");
			}
		});
		$("#panel").find("input[name='checkboxd']").attr("checked", false);
		$("#totalvalue").text("0");
	}
	//数字加千分符号
	function ver(n) {
		re = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g
		return n.replace(re, "$1,")
	}

	var first = 0;
	//取总的投注金蛋
	function getAllpceggs() {
		var total = 0;
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				var txt_value = $.trim($(this).val()).replace(/,/gi, "");
				if (txt_value && !isNaN(txt_value)) {
					total += parseInt(txt_value);
				}
			}
		})

		$("#totalvalue").text(ver(total + ""));

	}
//设置倍率
	function setAllvalue(peilv) {
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				var txt_value = $.trim($(this).val()).replace(/,/gi, "");
				if (txt_value && !isNaN(txt_value)) {
					var new_value = Math.floor(txt_value * peilv);
					$(this).val(ver(new_value + ""));
				}
			}
		});
	}
	//判断是否在数组中
	function isCon(arr, val) {
		for (var i = 0; i < arr.length; i++) {
			if (arr[i] == val) return true;
		}
		return false;
	}
	function refreshd() {
			for(var i = 0 ; i < 28 ; i++){
				if(getPeilv(i)>0){
					var index = i>13?27-i+14:i;
					$("#panel td img").eq(index).trigger('click');
				}
		}
	}
	function getPeilv(v){
		if((v%2==0 && localStorage.getItem("ds")=="s")||(v%2==1&&localStorage.getItem("ds")=="d"))
				return 1;
		if((v>13&&localStorage.getItem("dx")=="d")||(v<14&&localStorage.getItem("dx")=="x"))
				return 1;
		if((v%10>4&&localStorage.getItem("dxw")=="d")||(v%10<5&&localStorage.getItem("dxw")=="x"))
				return 1;
		if(((v>=10 && v <=17)&&localStorage.getItem("zb")=="z")||((v>17 || v<10)&&localStorage.getItem("zb")=="b"))
				return 1;
		return 0;
	}
	
	function task() {
		refreshd();
	}
	function comform() {
		t = $("#totalvalue").text().replace(/,/gi, "");
		t = parseInt(t);
		var str = [];
		for (var i = 0; i < 28; i++) {
			var txt_value = $.trim($("#txt" + i).val()).replace(/,/gi, "");
			str.push(txt_value);
		}
		$("#ALLSMONEY").val(str.join(","));
		t = ver(String(t)); //将数字转字符串后千分位 
		$("#postgoldeggs").html(t);
		$("#SMONEYSUM").val(t);
		$("input[name='SMONEY']").attr("disabled", false);

		var form1 = document.getElementById("form1"); //父层
		form1.submit();
	}
	function saveStorage( qihao ,vs){
		var v = vs.split("|");
		localStorage.setItem('ds','');
		localStorage.setItem('dx','');
		localStorage.setItem('dxw','');
		localStorage.setItem('zb','');
		for(i=0;i<v.length;i++){
			if(v[i] == "大"){
				localStorage.setItem('dx','d');
				return;
			}else if(v[i] == "小") {
				localStorage.setItem('dx','x');
				return;
			}else if(v[i] == "中") {
				localStorage.setItem('zb','z');
				return;
			}else if(v[i] == "边") {
				localStorage.setItem('zb','b');
				return;
			}else if(v[i] == "单") {
				localStorage.setItem('ds','d');
				return;
			}else if(v[i] == "双") {
				localStorage.setItem('ds','s');
				return;
			}else if(v[i] == "大尾") {
				localStorage.setItem('dxw','d');
				return;
			}else if(v[i] == "小尾") {
				localStorage.setItem('dxw','x');
				return;
			}
		}
	}
	 var url = document.URL;
	 if (url.indexOf('http://www.pceggs.com/play/pg28Insert.aspx?') != -1) {
	 		refreshd();
	 		setAllvalue(4);
	 		 comform();
	}else if (url.indexOf('http://www.pceggs.com/play/pc28.aspx') != -1) {
		var tds = $("#panel").find("tr:eq(5)").find("td");
		var _status = tds.eq(7).find("script").eq(0).text().trim().split("', '")[2].trim().replace("');",'');
		if(_status==0){	
			var url = "http://127.0.0.1:8000/GetPredictionFrom";
			var xhr = new XMLHttpRequest();
			xhr.open("GET", url, true);
			xhr.onreadystatechange = function() {
					if (xhr.readyState == 4) {
						var result = eval('(' + xhr.responseText + ')');
						var v = result.value;
						if(v != ''){
							saveStorage(result.qihao,v);
						//	var tds = $("#panel").find("tr:eq(5)").find("td");
						//	var _status = tds.eq(7).find("script").eq(0).text().trim().split("', '")[2].trim().replace("');",'');
						//	if(_status==0){
								LID = tds.eq(7).find("script").eq(0).text().trim().split("', '")[1].trim();
								window.location.href = "http://www.pceggs.com/play/pg28Insert.aspx?LID=" + LID;
							//}
						}
					}
			}
			xhr.send();
		}
	}
})();